//
//  PaperItemCellWebView.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/7/25.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperQuestionWebCell.h"

@interface PaperQuestionWebCell() <UIWebViewDelegate>

@property(nonatomic,strong)UIImageView *imgView; //显示题目
@property(nonatomic,strong)NSString *htmlString;
@property(nonatomic,weak)id<QuestionWebCellDelegate> delegate;
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)PaperQuestion *question;

@end

@implementation PaperQuestionWebCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.clipsToBounds = true;
//        self.backgroundColor = [UIColor blueColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIWebView *web = [UIWebView new];
        web.scrollView.scrollEnabled = false;
        web.scrollView.contentInset = UIEdgeInsetsZero;
        web.delegate = self;
        _webView = web;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _webView.frame = CGRectMake(0, 0, self.bounds.size.width, 44);
    [self.contentView addSubview: self.imgView];
}

-(void)setQuestionImage:(UIImage *)image{
    if (image) {
        _imgView.image = image;
        CGRect frame = _imgView.frame;
        frame.size.height = image.size.height;
        _imgView.frame = frame;
    }
}

// getter
-(UIImageView *)imgView{
    if (!_imgView) {
        UIImageView *iv = [[UIImageView alloc]initWithFrame: _webView.bounds];
        _imgView = iv;
    }
    return _imgView;
}

-(void)loadWebQuestionWithBaseUrl:(NSString *)url
                                question:(PaperQuestion *)question
                                delegate: (id)delegate{
    
    _question = question;
    _delegate = delegate;
    
    NSURL *baseURL = [NSURL fileURLWithPath:url];
    _webView.scalesPageToFit = false;
    [_webView loadHTMLString:_question.questionContent baseURL:baseURL];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
//    NSLog(@"question webcell load failed: %@",error.localizedDescription);
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return !_question.image;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (webView == _webView) {
        CGFloat h = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
        
        if (h > 0) {
            CGRect frame = webView.frame;
            frame.size.height = h + 5;
            webView.frame = frame;
            
            _imgView.frame = frame;
            
            // 生成image
            _question.image = [self viewRenderToImage: _webView];
            [_delegate questionWebCellContentDidLoadWithQuestion:_question];
        }
    }
}

/**
 *  UIView to UIImage
 */
-(UIImage*)viewRenderToImage: (UIView*)view{
    CGSize size = view.bounds.size;
    UIGraphicsBeginImageContextWithOptions(size, true, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
//    [UIImagePNGRepresentation(image) writeToFile:[NSHomeDirectory() stringByAppendingString:@"/Documents/test.png"] atomically:true];
    return image;
}


@end


